package cn.spring.controller;

import cn.spring.base.utils.PageInfo;
import cn.spring.domain.Department;
import cn.spring.domain.Employee;
import cn.spring.query.DepartmentQuery;
import cn.spring.service.IDepartmentService;
import cn.spring.base.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Api(tags = "部门管理")
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    //新增 更新
    @PutMapping
    @ApiOperation(value = "新增/修改")
    public AjaxResult saveOrUpdate(@RequestBody Department department) {
        AjaxResult ajaxResult = AjaxResult.me();
        try {
            if (Objects.isNull(department.getId())) {
                department.setCreateTime(new Date());
                department.setUpdateTime(new Date());
                System.out.println(new Date());
                service.insert(department);
                return ajaxResult.me();
            } else {
                department.setUpdateTime(new Date());
                service.update(department);
                return ajaxResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }

    //删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个")
    public AjaxResult delete(@PathVariable("id") Long id) {
        AjaxResult ajaxResult = AjaxResult.me();
        try {
            service.delete(id);
            return ajaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }

    //查询全部
    @GetMapping
    @ApiOperation(value = "获取所有")
    public AjaxResult getAll() {
        AjaxResult ajaxResult = AjaxResult.me();
        try {
            List<Department> departments = service.selectAll();
            return ajaxResult.me().setResultObj(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }

    //查询单个数据
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public AjaxResult getOne(@PathVariable("id") Long id) {
        AjaxResult ajaxResult = AjaxResult.me();
        try {
            Department department = service.selectById(id);
            return ajaxResult.me().setResultObj(department);
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }

    //删除多个数据
    @PatchMapping
    @ApiOperation(value = "删除多个数据")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        AjaxResult ajaxResult = AjaxResult.me();
        try {
            service.batchDelete(ids);
            return ajaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return ajaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }

    //高级查询和分页
    @PostMapping
    @ApiOperation(value = "高级查询与分页功能")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo<Department> list = service.queryPage(departmentQuery);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }
}
